package de.recipeminer.crawling.chefkoch;

import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import de.recipeminer.models.chefkoch.ChefkochIngredient;
import de.recipeminer.models.chefkoch.ChefkochIngredientLineItem;
import de.recipeminer.models.chefkoch.ChefkochRecipe;
import de.recipeminer.models.chefkoch.ChefkochUnit;
import de.recipeminer.tools.Tools;
import java.nio.charset.CharacterCodingException;

import java.util.List;

/**
 * @author Markus Ackermann
 */
public class ChefkochJsonConverter {
    private static Gson gson;
    

    public ChefkochRecipe convert(ChefkochJsonRecipe jsonRecipe) {
        RecipeJsonStructure recipeStruct = parse(jsonRecipe.getJsonData());
        long chefkochId = recipeStruct.rezept_id;
        String title = recipeStruct.rezept_name;
        String instructions = recipeStruct.rezept_zubereitung;
        long prepTime = recipeStruct.rezept_preparation_time;
        String difficulty = recipeStruct.rezept_schwierigkeit;
        long servings = recipeStruct.rezept_portionen;

        ChefkochRecipe recipe = new ChefkochRecipe(jsonRecipe.getUUID(), chefkochId, title, instructions, prepTime,
                difficulty, servings);
        List<ChefkochIngredientLineItem> ingLineList = Lists.newLinkedList();
        //TODO: Ingredients are apperently not properly aggregated into the recipe
        //TODO: for a sound data model, Chefkoch should aggregate ChefkochIngredientLineItems
        for(IngredientLineJsonStructure ingStruct : recipeStruct.rezept_zutaten) {
            ChefkochIngredient ing = new ChefkochIngredient(ingStruct.id, ingStruct.name);
            //TODO: Do not save empty eigenschaft strings
            ing.setAnnotation(ChefkochIngredient.Keys.ADDITIONAL_DESCRIPTION, ingStruct.eigenschaft);
            ing.setAnnotation(ChefkochIngredient.Keys.BASIC, ingStruct.ist_basic > 0);
            ing.setAnnotation(ChefkochIngredient.Keys.ADDITIONAL_INFO, ingStruct.has_additional_info);
            
            ChefkochIngredientLineItem ingLine;
            ingLine = recipe.addIngredientLineItem(ing, ingStruct.menge, ChefkochUnit.UNSPECIFIED);
            ingLine.setAnnotation(ChefkochIngredientLineItem.Keys.UNIT_STRING, ingStruct.einheit);
            ingLine.setAnnotation(ChefkochIngredientLineItem.Keys.ADDITIONAL_DESCRIPTION, ingStruct.eigenschaft);
            ingLine.setAnnotation(ChefkochIngredientLineItem.Keys.ADDITIONAL_INFO, ingStruct.has_additional_info);

        }
        recipe.setAnnotation(ChefkochRecipe.Keys.SUBTITLE, recipeStruct.rezept_name2);
        recipe.setAnnotation(ChefkochRecipe.Keys.SHOW_ID, recipeStruct.rezept_show_id);
        recipe.setAnnotation(ChefkochRecipe.Keys.COOKING_TIME, recipeStruct.rezept_cooking_time);
        recipe.setAnnotation(ChefkochRecipe.Keys.RESTING_TIME, recipeStruct.rezept_resting_time);
        return recipe;
    }

    private RecipeJsonStructure parse(String jsonData) {
        if(gson == null)
            gson = new GsonBuilder().create();

        try
        {
        jsonData = Tools.transcodeChefkochString(jsonData);
        }
        catch(CharacterCodingException e)
        {
            System.out.println("exception while trying to translate the chefkoch string");
            e.printStackTrace();
        }
        RecipeJsonStructure newrecipe = gson.fromJson(jsonData, ChefkochJsonConverter.RecipeJsonStructure.class);
/*
        newrecipe.rezept_name;
        newrecipe.rezept_name2;
        newrecipe.rezept_zubereitung;
        newrecipe.rezept_zutaten;*/

       return newrecipe;

    }

    private ChefkochIngredient extractIngredient(IngredientLineJsonStructure lineStruct) {
        return new ChefkochIngredient(lineStruct.id, lineStruct.name);
    }

 /*   private String convertFromChefkochEncodingToUTF(String cs)
    {
        
        String newstring = "";
        newstring = cs;

        return newstring;
    } */

    private static class RecipeJsonStructure {
        String rezept_name, rezept_name2, rezept_show_id;
        String rezept_zubereitung, rezept_schwierigkeit;
        long rezept_id, rezept_portionen;
        long rezept_preparation_time, rezept_cooking_time, rezept_resting_time;
        List<Long> rezept_zutaten_is_basic;
        List<IngredientLineJsonStructure> rezept_zutaten;
    }

    private static class IngredientLineJsonStructure {
        long id;
        float menge;
        String name, eigenschaft, einheit;
        short ist_basic;
        boolean has_additional_info;
    }
}
